<?php
/**
 * 项目相关处理类
 * @author liubin-bt
 * @property Project_model $Project_model
 * @property User_model $User_model
 * @property ProjectApply_model $ProjectApply_model
 * @property ProjectInvestorRecom_model $ProjectInvestorRecom_model
 */
class Project extends Base{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('Project_model');
        $this->config->load('projectstatus');
    }
    
    public function getList() {
        $page = intval($this->getSafeRequest('page', 1));
        $type = intval($this->getSafeRequest('type', 0));
        list($count, $list) = $this->Project_model->gePageList($type, $page);
        
        $finance_rotations = $this->config->item('finance_rotation');
        $citys = $this->getCityInfos();
        $managers = array_column($list, 'manager');
        $managers = array_filter($managers, 'intval');
        $managers = array_unique($managers);
        $userinfos = [];
        if( $managers ){
            $this->load->model('User_model');
            $userinfos = $this->User_model->userAuth( $managers );
        }
        foreach ($list as &$v){
            $v['project_mark'] = preg_split('/[,，；;]+/is', $v['project_mark']);
            $v['finance_rotation'] = isset($finance_rotations[$v['finance_rotation']]) ? $finance_rotations[$v['finance_rotation']] : '';
            $city_ids = json_decode($v['company_area'], true);
            $v['company_area'] = $this->getCityByIds($citys, $city_ids);
            $v['project_cover'] = json_decode($v['project_cover'], true);
            $v['project_team'] = json_decode($v['project_team'], true);
            $v['manager'] = isset($userinfos[$v['manager']]) ? $userinfos[$v['manager']] : [];
        }
        $this->outJsonData(0, ['count'=>$count, 'list'=>$list]);
    }
    
    /**
     * 项目详情接口
     */
    public function detail(){
        $project_id = intval($this->getSafeRequest('id', 1));
        if( !$project_id ){
            $this->outJsonData(0, '项目id不能为空');
        }
        $fields = 'id,project_name,company_name,company_desc,company_legal,company_regaddr,project_team,finance_history,
            company_url,company_area,company_addr,company_industry,project_mark,project_creator,
            finance_rotation,finance_expect,project_cover,project_desc,company_bussiness,ctime';
        $project = $this->Project_model->getProjectOne($project_id, $fields);
        if( empty($project) ){
            $this->outJsonData(0, '该项目不存在');
        }
        $city_ids = json_decode($project['company_area'], true);
        $citys = $this->getCityInfos();
        $project['company_area'] = $this->getCityByIds($citys, $city_ids);
        $project['project_team'] = json_decode($project['project_team'], true);
        $project['project_cover'] = !empty($project['project_cover']) ? json_decode($project['project_cover'], true) : [];
        $finance_rotations = $this->config->item('finance_rotation');
        $project['finance_rotation'] = isset($finance_rotations[$project['finance_rotation']]) ? $finance_rotations[$project['finance_rotation']] : '';
        $finance_history = json_decode($project['finance_history'], true);
        foreach ($finance_history as &$h){
            $h['finance_round'] = isset($finance_rotations[$h['finance_round']]) ? $finance_rotations[$h['finance_round']] : '';
        }
        $project['finance_history'] = $finance_history;
        $project['project_mark'] = preg_split('/[,，；;]+/is', $project['project_mark']);
        $this->load->model('ProjectApply_model');
        $apply = $this->ProjectApply_model->getOneByUid( $this->uid );
        $roadshows = $this->Project_model->getRoadShow( $project_id );
        $project['files_taser'] = [];
        $project['files_bp'] = [];
        $isgroup = $this->auth_invaid($project_id);
        if( $isgroup || (isset($apply[1]) && $apply[1]['status']==1) ){
            //taser
            if( !empty($roadshows['files_taser']) ){
                $project['files_taser'] = json_decode($roadshows['files_taser'], true);
            }
        }
        if( $isgroup || (isset($apply[2]) && $apply[2]['status']==1) ){
            //bp
            if( !empty($roadshows['files_bp']) ){
                $project['files_bp'] = json_decode($roadshows['files_bp'], true);
            }
        }
        $project['is_member'] = $isgroup ? 1 : 0;
        $project['up_feedback_time'] = !empty($project['up_feedback_time']) ? $project['up_feedback_time'] : date('Y-m-d H:i:s', $project['ctime']);
        $this->outJsonData(0, $project);
    }
    /**
     * 申请查看teaser和bp
     * type值：1：teaser 2：bp
     */
    public function applyView(){
        $project_id = intval($this->getSafeRequest('id', 1));
        $type = intval($this->getSafeRequest('type', 1));
        if( !$project_id ){
            $this->outJsonData(0, '项目id不能为空');
        }
        $this->load->model('ProjectApply_model');
        $apply = $this->ProjectApply_model->get( ['uid'=>$this->uid, 'type'=>$type] );
        if( $apply ){
            $this->outJsonData(0, '正在审核，请勿重复申请');
        }
        $data['project_id'] = $project_id;
        $data['type'] = $type;
        $data['uid'] = $this->uid;
        $res = $this->ProjectApply_model->insert($data);
        
        $this->outJsonData(0, '申请成功，请耐心等待');
    }
    
    public function roadShow(){
        $project_id = intval($this->getSafeRequest('id'));
        $this->load->model('ProjectInvestorRecom_model');
        list($count, $list) = $this->ProjectInvestorRecom_model->gePageList($project_id, 1, 1000);
        
        $this->config->load('projectstatus');
        $recomm_status = $this->config->item('recomm_status');
        //列表分组
        $grouplist['recomm_count'] = 0;
        $grouplist['meeting_count'] = 0;
        $grouplist['roadshow_count'] = 0;
        $grouplist['recomm_list'] = [];
        $grouplist['meeting_list'] = [];
        $grouplist['roadshow_list'] = [];
        $fbcount = [];
        foreach ($list as $v){
            $temp = ['id' => $v['id'],
                'instustry_name' => $v['instustry_name'],
                'job' => $v['job'],
                'investor' => $v['investor'],
                'next' => $v['next'],
                'addr' => $v['addr'],
                'status_name'=>isset($recomm_status[$v['status']]) ? $recomm_status[$v['status']] : ''
            ];
            $fbcount[$v['fb_status']][] = $temp;
            if( in_array($v['status'], ['1','2','3','4']) ){
                $grouplist['recomm_list'][] = $temp;
            } else if( in_array($v['status'], ['5','6','7']) ){
                $grouplist['meeting_list'][] = $temp;
            } else {
                $temp['fb_status'] = !empty($v['fb_status']) ? $v['fb_status'] : '';
                $grouplist['roadshow_list'][] = $temp;
            }
        }
        $grouplist['recomm_count'] = count($grouplist['recomm_list']);
        $grouplist['meeting_count'] = count($grouplist['meeting_list']);
        $grouplist['roadshow_count'] = count($grouplist['roadshow_list']);
        
        $this->outJsonData(0, $grouplist);
    }
}